// JavaScript Document
// cancella gli spazi prima e dopo una stringa
String.prototype.trim = function () 
{
    return this.replace(/^\s+|\s+$/, "");
}
/*
 *  Raccolta di metodi di uso comune e generale in tutto il sito 
 */
var Common = 
{
    // registrazione di eventi compatibile con tutti i browser
    addEvent : function (node, evType, funct) 
    {
        if (node.addEventListener) {
            node.addEventListener(evType, funct, false);
            //alert("test ff " + this);
            return true;
        }
        else if (node.attachEvent) {
            //alert("test ie " + this);
            return node.attachEvent("on" + evType, funct);
        }
        else {
            //alert("test no " + this);
            return false;
        }
    },
    /*
    * Mostra o nasconde un elemento
    */
    toggle : function (element) 
    {
        var s = null;
        if (window.getComputedStyle) 
        {
            element.style.display = (window.getComputedStyle(element, null).display == "none") ? "block" : "none";
        }
        else if (element.currentStyle) 
        {
            alert("IE");
            element.style.display = (element.currentStyle.display == "none") ? "block" : "none";
        }
        return false;
    },
    findPos : function (obj) 
    {
        var curleft = curtop = 0;
        if (obj.offsetParent) {
            do {
                curleft += obj.offsetLeft;
                curtop += obj.offsetTop;
            }
            while (obj = obj.offsetParent);
        }
        return [curleft, curtop];
    },
    /*
    * 
    */
    showTooltip : function (el_id, type) 
    {
        if (document.getElementById("tooltip") != null) 
        {
            document.getElementsByTagName("body")[0].removeChild(document.getElementById("tooltip"));
        }
        var origin = document.getElementById(el_id);
        var tooltip_base = document.createElement("div");
        tooltip_base.id = "tooltip";
        tooltip_base.className = "tooltip_base";
        if (type == "front") 
        {
            tooltip_base.style.top = String(Common.findPos(origin)[1] + 15) + "px";
            tooltip_base.style.left = String(Common.findPos(origin)[0] - 20) + "px";
            var tooltip_spike = document.createElement("img");
            tooltip_spike.src = "../images/tooltip_spike.png";
            tooltip_base.appendChild(tooltip_spike);
        }
        if (type == "rear") 
        {
            tooltip_base.style.top = String(Common.findPos(origin)[1] + 15) + "px";
            tooltip_base.style.left = String(Common.findPos(origin)[0] - 230) + "px";
            var tooltip_spike = document.createElement("img");
            tooltip_spike.src = "../images/tooltip_spike.png";
            tooltip_base.appendChild(tooltip_spike);
        }
        var h_s = origin.getAttribute("rel");
        /*
        if (el_id == "feed_wrapper_help") 
        {
        h_s = "This column is the list of all the feeds you previously subscribed. Click on one of them
        to have, here on the right, the list of all its items";
        }
        if (el_id == "feed_items_help") 
        {
        h_s = "In this column you have the list of all the items of the feed you selected on the left.
        Click one of them to see its content";
        }
        if (el_id == "feed_content_help") 
        {
        h_s = "Here is the content of the item you previously selected. Maybe the content you see here
        is not the whole content of the article. To be sure, follow the link under the title to travel
        to the original post.";
        }*/
        var helpString = document.createTextNode(h_s);
        var par = document.createElement("p");
        par.appendChild(helpString);
        tooltip_base.appendChild(par);
        document.getElementsByTagName("body")[0].appendChild(tooltip_base);
        return false;
    },
    /*
    * 
    */
    clearTooltip : function (el_id) 
    {
        if (document.getElementById("tooltip") != null) 
        {
            document.getElementsByTagName("body")[0].removeChild(document.getElementById("tooltip"));
        }
    },
    /*
     * Literals per controllare la gestione dei cookies 
     */
    Cookies : 
    {
        // funzione per creare un cookie
        createCookie : function (name, value, days) 
        {
            if (days) 
            {
                var date = new Date();
                date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
                var expires = "; expires=" + date.toGMTString();
            }
            else {
                var expires = "";
            }
            document.cookie = name + "=" + value + expires + "; path=/";
        },
        // legge un cookie e ne ritorna il valore, altrimenti null se non esiste
        readCookie : function (name) 
        {
            var nameEQ = name + "=";
            var ca = document.cookie.split(';');
            for (var i = 0; i < ca.length; i++) 
            {
                var c = ca[i];
                while (c.charAt(0) == ' ') {
                    c = c.substring(1, c.length);
                }
                if (c.indexOf(nameEQ) == 0) {
                    return c.substring(nameEQ.length, c.length);
                }
            }
            return null;
        },
        // cancella un cookie sovrascrivendolo
        eraseCookie : function (name) 
        {
            createCookie(name, "", - 1);
        }
    }
};
